import bcrypt from 'bcryptjs';
import jwt, { JwtPayload } from 'jsonwebtoken';

export async function hashPassword(password: string) {
  return await bcrypt.hash(password, 10);
}

export async function comparePasswords(password: string, hashedPassword: string) {
  return await bcrypt.compare(password, hashedPassword);
}

export function generateToken(userId: string) {
  return jwt.sign(
    { userId },
    process.env.JWT_SECRET || 'fallback-secret',
    { expiresIn: '7d' }
  );
}

export function verifyToken(token: string) {
  try {
    console.log('Verifying token:', token.substring(0, 10) + '...');
    const payload = jwt.verify(token, process.env.JWT_SECRET!) as JwtPayload;
    console.log('Token payload:', payload);
    return payload;
  } catch (error) {
    console.error('Token verification error:', error);
    return null;
  }
} 